package jschool.manejadores;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import jschool.persistencia.Conexion;

public class ManejadorDeValorDeNomina {
	private static ManejadorDeValorDeNomina instancia = null;
	private Double[] valores = null;
	private Conexion conexion = null;
	private ResultSet rs = null;
	private PreparedStatement stmt = null;

	private ManejadorDeValorDeNomina() {
		conexion = Conexion.getInstancia();
	}

	public static ManejadorDeValorDeNomina getInstancia() {
		if (instancia == null) {
			instancia = new ManejadorDeValorDeNomina();
		}
		return instancia;
	}

	public Double[] getValores() {
		valores = new Double[4];
		try {
			String sql = "SELECT * FROM valornomina";
			stmt = (PreparedStatement) conexion.hacerEnunciado(sql);
			rs = conexion.hacerConsulta(sql);
			while (rs.next()) {
				valores[1] = rs.getDouble(1);
				valores[2] = rs.getDouble(2);
				valores[3] = rs.getDouble(3);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					"Error Listando Valores de Nomina", "Error",
					JOptionPane.ERROR_MESSAGE);
			return null;
		}
		return valores;
	}

	public int modificarValorNomina(double isr, double ars, double sp) {
		int fila = 0;
		try {

			String sql = "UPDATE valornomina SET isr = ?, ars = ?, " + "sp = ?";
			stmt = (PreparedStatement) conexion.hacerEnunciado(sql);
			stmt.setDouble(1, isr);
			stmt.setDouble(2, ars);
			stmt.setDouble(3, sp);
			fila = stmt.executeUpdate();
			JOptionPane.showMessageDialog(null, "Valor Modificado",
					"Modificacion Exitosa", JOptionPane.INFORMATION_MESSAGE);
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					"Error Modificando Valor de Nomina", "Error",
					JOptionPane.ERROR_MESSAGE);
			return 0;
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return fila;
	}

}
